package cn.tedu.mall.order.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.order.service.imp.OmsCartServiceImpl;
import cn.tedu.mall.order.utils.WebConsts;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@RestController
@RequestMapping("/mos/cart")
@Api(tags = "购物车功能")
public class OmsCartController {

    @Autowired
    private IOmsCartService omsCartService;

    @PostMapping("/add")
    @ApiOperation("当前登录用户新增sku到购物车")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult add(@Validated CartAddDTO cartAddDTO){
        omsCartService.addCart(cartAddDTO);
        return JsonResult.ok("添加购物车完成");
    }

    // leaf   passport    order
    // sso:10002   order:10005
    // 先运行10002的knife4j 访问前台用户登录 登录成功复制jwt
    // 转到order10005,在全局设置中设置参数
    // name:   Authorization
    // value:  Beaer [复制的jwt]
    // 然后要刷新10005的knife4j页面
    // 就可以发送新增到购物车的请求了

    @GetMapping("/list")
    @ApiOperation("查询当前用户的购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",required = false,dataType = "int"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",required = false,dataType = "int")
    })
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult<JsonPage<CartStandardVO>> listCart(@RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE) Integer page,
                                                         @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE) Integer pageSize){
        JsonPage<CartStandardVO> list = omsCartService.listCarts(page, pageSize);
        return JsonResult.ok(list);
    }

    @PostMapping("/delete")
    @ApiOperation("更具用户选择的购物车id删除购物车信息(支持批量)")
    @ApiImplicitParam(value = "购物车id",name = "ids",required = true,dataType = "array")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult removeCartById(Long[] ids){
        omsCartService.removeCart(ids);
        return JsonResult.ok("删除成功");
    }

    @PostMapping("/delete/all")
    @ApiOperation("根据用户id清空该用户购物车列表")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult removeAllCart(){
        omsCartService.removeAllCarts();
        return JsonResult.ok("清空购物车成功");
    }

    @PostMapping("/update/quantity")
    @ApiOperation("更新购物车数量")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult updateQuantity(CartUpdateDTO cartUpdateDTO){
        omsCartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok("购物车数量更新成功");
    }
}
